package study;

import cn.hutool.json.JSONUtil;
import org.apache.http.client.HttpClient;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import study.entity.HttpVo;
import study.utils.JsonUtils;

import javax.swing.text.html.parser.Entity;
import java.io.IOException;
import java.util.Objects;

/**
 * @Author 刘伟涛
 * @Date 2024/1/11 10:55
 **/
public class HttpDemo1 {
    public static void main(String[] args) {
        CloseableHttpClient httpClient = HttpClients.createDefault();

        CloseableHttpResponse response = null;
        String string = "";
        HttpVo httpVo = new HttpVo();
        httpVo.setPage(1);
        httpVo.setLimit(10);
        httpVo.setCkid("2311081012555183");
        httpVo.setPdid("2401081800001161");

/*        HttpPost post = new HttpPost("http://localhost:9527/api/materialio/wuliaoPd/checkPdDetail");
        post = (HttpPost) addHeaders(post, "Authorization", "4e4fb24a7b8f47ddac0de1080828cdc0");
        post = (HttpPost) addHeaders(post, "SystemId", "SYSTEM_MATERIAL");
        post = (HttpPost) addHeaders(post, "Companyid", "5666d470b79c5a27bbe0f10090d497c2");*/
        HttpGet post = new HttpGet("http://localhost:8222/admin/acl/test/config");
        post = (HttpGet) addHeaders(post, "Token", "eyJhbGciOiJIUzUxMiIsInppcCI6IkdaSVAifQ.H4sIAAAAAAAAAKtWKi5NUrJSSkzJzcxT0lFKrShQsjI0NzA1sjSwMDOsBQCzF0W9IAAAAA.5lvVp2fGR3cXiEcg5EQwYGR05HOMKO6KK4bC8x2aHNYVzhzf1G04rjCDlvbQucY40p3XobJ2qF0KTzI2cZfmuw");
        post = (HttpGet) addHeaders(post, "SystemId", "SYSTEM_MATERIAL");
        post = (HttpGet) addHeaders(post, "Companyid", "5666d470b79c5a27bbe0f10090d497c2");
//        StringEntity entity = new StringEntity(Objects.requireNonNull(JsonUtils.objectToJson(httpVo)), ContentType.APPLICATION_JSON);
//        post.setEntity(entity);
        try {
            response = httpClient.execute(post);
            string = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(string);
    }

    private static HttpRequestBase addHeaders(HttpRequestBase httpRequest, String key, String value) {
        // 设置请求头信息，鉴权
//        httpRequest.setHeader("Authorization", "Bearer da3efcbf-0845-4fe3-8aba-ee040be542c0");
        httpRequest.setHeader(key, value);
        // 设置配置请求参数
        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 连接主机服务超时时间
                .setConnectionRequestTimeout(35000)// 请求超时时间
                .setSocketTimeout(60000)// 数据读取超时时间
                .build();
        // 为httpGet实例设置配置
        httpRequest.setConfig(requestConfig);
        return httpRequest;
    }
}
